{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Reverse Words in a String\n",
    "\n",
    "[原题](https://mp.weixin.qq.com/s/7g2oUrzDHpc76rZGm2TsgQ)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.\n",
    "\n",
    "```text\n",
    "Input: \"The cat in the hat\"\n",
    "Output: \"ehT tac ni eht tah\"\n",
    "```\n",
    "\n",
    "**Note:** *In the string, each word is separated by single space and there will not be any extra space in the string.*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution:\n",
    "    ''' The Solution Class '''\n",
    "    \n",
    "    def reverseWords(self, string: str):\n",
    "        ''' The Main Algorithm '''\n",
    "        \n",
    "        # Split the whole string by its space.\n",
    "        words_list = string.split(' ')\n",
    "        \n",
    "        # Reverse every word in the list.\n",
    "        words_list = [word[-1::-1] for word in words_list]\n",
    "        \n",
    "        # Combine them together.\n",
    "        return ' '.join(words_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ehT tac ni eht tah\n"
     ]
    }
   ],
   "source": [
    "print(Solution().reverseWords('The cat in the hat'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
